package com.lijiayue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lijiayue.mapper.MentalDataMapper;
import com.lijiayue.model.domain.MentalData;
import com.lijiayue.service.MentalDataService;
import com.lijiayue.util.AuthContextHolder;
import com.lijiayue.util.Result;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MentalDataServiceImpl extends ServiceImpl<MentalDataMapper, MentalData> implements MentalDataService {

    @Resource
    private MentalDataMapper mentalDataMapper;


    @Override
    public MentalData getMentalDataHistory() {
        Long userId = AuthContextHolder.getUserId();
        LambdaQueryWrapper<MentalData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MentalData::getUserId, userId)
                .orderByDesc(MentalData::getCreateTime);
        MentalData mentalData = mentalDataMapper.selectList(queryWrapper).get(0);
        if (ObjectUtils.isNotEmpty(mentalData)){
            return mentalData;
        }
        throw new RuntimeException("没有查询到数据");
    }

    @Override
    public List<MentalData> getList() {
        Long userId = AuthContextHolder.getUserId();
        LambdaQueryWrapper<MentalData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MentalData::getUserId, userId);
        List<MentalData> mentalDataList = mentalDataMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(mentalDataList)){
            throw new RuntimeException("没有查询到数据");
        }
        return mentalDataList;
    }
}
